package com.example.wanxiang.controller;

import com.example.wanxiang.dto.MessageDto;
import com.example.wanxiang.dto.MessageReturn;
import com.example.wanxiang.entity.Chat;
import com.example.wanxiang.entity.Message;
import com.example.wanxiang.service.MessageService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @GetMapping("/findMessagesByChatId")
    public MessageReturn findMessagesByChatId(@RequestBody MessageDto messageDto) {
        MessageReturn messageReturn = new MessageReturn();
        messageReturn.setMessages(messageService.findMessagesByChatId(messageDto));
        messageReturn.setCount(messageService.getMessageCount(messageDto));
        messageReturn.setPageNow(messageDto.getPage());
        messageReturn.setPageSize(messageDto.getSize());
        return messageReturn;
    }

    @PostMapping("/save")
    public Message save(@RequestBody MessageDto messageDto) {
        Message message = new Message();
        message.setContent(messageDto.getContent());
        message.setChatId(message.getChatId());
        message.setSendTime(LocalDateTime.now());
        message.setId(messageDto.getId());
        message.setSendId(messageDto.getSendId());
        return messageService.saveMessage(message);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        messageService.deleteMessage(id);
    }

}
